package com.wuming.caiwu.service.impl;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.WumingErrorCode;
import com.wuming.caiwu.dao.DmsOrderMapper;
import com.wuming.caiwu.model.DmsOrder;
import com.wuming.caiwu.model.FinanceReport;
import com.wuming.caiwu.service.DmsOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DmsOrderServiceImpl extends BaseServiceImpl implements DmsOrderService {

    @Autowired
    private DmsOrderMapper dmsOrderMapper;

    @Override
    public void removeOrderById(Long id) throws BusinessException {
        dmsOrderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public BigDecimal getProfitsharingMoney(String startDate, String endDate, String orderSn, Long storeId) {
        Map<String,Object> params = new HashMap<>();
        if(null != storeId){
            params.put("storeId",storeId);
        }
        if(StringUtils.isNotBlank(orderSn)){
            params.put("orderSn",orderSn);
        }
        if(StringUtils.isNotBlank(startDate)){
            params.put("startDate",startDate + " 00:00:00");
        }
        if(StringUtils.isNotBlank(endDate)){
            params.put("endDate",endDate + " 23:59:59");
        }
        return dmsOrderMapper.selectProfitSharingMoney(params);
    }

    @Override
    public List<DmsOrder> getDmsOrderByPage(Long storeId, String orderSn, Integer orderStatus, String startDate,String endDate,Integer payType,int pageIndex, int pageSize) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(null != storeId){
            params.put("storeId",storeId);
        }
        if(StringUtils.isNotBlank(orderSn)){
            params.put("orderSn",orderSn);
        }
        if(null != orderStatus){
            params.put("orderStatus",orderStatus);
        }
        if(StringUtils.isNotBlank(startDate)){
            params.put("startDate",startDate + " 00:00:00");
        }
        if(StringUtils.isNotBlank(endDate)){
            params.put("endDate",endDate + " 23:59:59");
        }
        params.put("payType",payType);
        try{
            return (List<DmsOrder>) this.getPageList(DmsOrderMapper.class, "selectByParams", params, pageIndex, pageSize);
        }catch(Exception e) {
            logger.error("查询订单信息失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(Long storeId, String orderSn, Integer orderStatus,String startDate,String endDate,Integer payType) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(null != storeId){
            params.put("storeId",storeId);
        }
        if(StringUtils.isNotBlank(orderSn)){
            params.put("orderSn",orderSn);
        }
        if(null != orderStatus){
            params.put("orderStatus",orderStatus);
        }
        if(StringUtils.isNotBlank(startDate)){
            params.put("startDate",startDate + " 00:00:00");
        }
        if(StringUtils.isNotBlank(endDate)){
            params.put("endDate",endDate + " 23:59:59");
        }
        params.put("payType",payType);
        try{
            return this.getTotal(DmsOrderMapper.class, "selectTotal", params);
        }catch (Exception e){
            logger.error("查询订单信息总数失败.异常:{}", e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public List<FinanceReport> getFinanceReportByPage(Long storeId, String startDate, String endDate, Integer payType, int pageIndex, int pageSize) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(null != storeId){
            params.put("storeId",storeId);
        }
        if(StringUtils.isNotBlank(startDate)){
            params.put("startDate",startDate + " 00:00:00");
        }
        if(StringUtils.isNotBlank(endDate)){
            params.put("endDate",endDate + " 23:59:59");
        }
        params.put("payType",payType);
        try{
            return (List<FinanceReport>) this.getPageList(DmsOrderMapper.class, "selectFianceReport", params, pageIndex, pageSize);
        }catch(Exception e) {
            logger.error("查询订单信息失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getFinanceReportTotal(Long storeId, String startDate, String endDate, Integer payType) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(null != storeId){
            params.put("storeId",storeId);
        }
        if(StringUtils.isNotBlank(startDate)){
            params.put("startDate",startDate + " 00:00:00");
        }
        if(StringUtils.isNotBlank(endDate)){
            params.put("endDate",endDate + " 23:59:59");
        }
        params.put("payType",payType);
        try{
            return  this.getTotal(DmsOrderMapper.class, "selectFianceReportTotal", params);
        }catch(Exception e) {
            logger.error("查询订单信息失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }
}
